package com.xiaotu.easypoi.testlombok;

import com.xiaotu.easypoi.testexport.model.ExportUserModel;
import lombok.Synchronized;

import java.awt.image.VolatileImage;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author 张晓
 * @create 2021-05-20 15:20
 * <p>
 * 测试注解：
 * @Synchronized : 同步方法安全的转化
 */
public class TestSynchronizedAnnotation {


    private volatile int num;

    private volatile ExportUserModel userModel;

//    private int num;

    //    @Synchronized
    public void setNum() {
        //this.num++;
        System.out.println("当前线程编号为：" + Thread.currentThread().getName() + "  当前编号为：" + ++num);
    }

    public void printNum() {
        System.out.println("当前线程编号为：" + Thread.currentThread().getName() + "  当前编号为：" + this.num);
    }

    public static void main(String[] args) {
        TestSynchronizedAnnotation sync = new TestSynchronizedAnnotation();

        ExportUserModel userModel = new ExportUserModel();

        ExecutorService pool = Executors.newFixedThreadPool(2);
        for (int i = 0; i < 2; i++) {
            pool.execute(() -> sync.setNum());
//            pool.execute(() -> {
//                sync.setNum();
//                sync.printNum();
//            });
        }
    }

}
